package com.hengpeng.msls.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * <pre>
 * 微信请求校验工具类
 * </pre>
 * 
 * @author shenshaopeng
 * @version 1.0, 2014-3-4
 */
public class SignUtil {
    // 与接口配置信息中的Token要一致
    private static String token = "fcdtWeixinCourse";
    private static String szfcToken = "szfcWeixinCourse";
    private static String shfcToken = "shfcWeixinCourse";
    private static String tcdtToken = "tcdtWeixinCourse"; // 体彩定投
    private static String szfcDyToken = "szfcDyWeixinCourse"; // 深圳福彩订阅号
    private static String rcpToken = "rcapWeixinCourse"; // 瑞彩彩票
    private static String happyRuiCai = "happyRuiCaiWeixinCourse"; // 开心瑞彩
    private static String happyCaipiao = "happyCaiPiaoWeixinCourse"; // 开心彩票
    private static String xiangyuCaipiao = "xiangyuCaiWeixinCourse"; // 开心吉祥
    private static String aocairuiCaipiao = "aocairuiWeixinCourse"; // 奥彩瑞

    /**
     * 验证签名
     * 
     * @param signature
     * @param timestamp
     * @param nonce
     * @return
     */
    public static boolean checkSignature(String signature, String timestamp, String nonce, String type) {
        String tok = token;
        if ("1001".equals(type)) { // 深圳福彩token
            tok = szfcToken;
        } else if ("1002".equals(type)) { // 体彩定投token
            tok = tcdtToken;
        } else if ("1003".equals(type)) {
            tok = shfcToken; // 上海福彩
        } else if ("1004".equals(type)) {
            tok = szfcDyToken;
        } else if ("1005".equals(type)) {
            tok = rcpToken;
        } else if ("1006".equals(type)) {
            tok = happyRuiCai;
        } else if ("1007".equals(type)) {
            tok = happyCaipiao;
        } else if ("1008".equals(type)) {
            tok = xiangyuCaipiao;
        } else if ("1009".equals(type)) {
            tok = aocairuiCaipiao;
        }
        String[] arr = new String[] { tok, timestamp, nonce };
        // 将token、timestamp、nonce三个参数进行字典序排序
        Arrays.sort(arr);
        StringBuilder content = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            content.append(arr[i]);
        }
        MessageDigest md = null;
        String tmpStr = null;

        try {
            md = MessageDigest.getInstance("SHA-1");
            // 将三个参数字符串拼接成一个字符串进行sha1加密
            byte[] digest = md.digest(content.toString().getBytes());
            tmpStr = byteToStr(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        content = null;
        // 将sha1加密后的字符串可与signature对比，标识该请求来源于微信
        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
    }

    /**
     * 将字节数组转换为十六进制字符串
     * 
     * @param byteArray
     * @return
     */
    private static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符串
     * 
     * @param mByte
     * @return
     */
    private static String byteToHexStr(byte mByte) {
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];

        String s = new String(tempArr);
        return s;
    }
}
